/// <reference path="../lib/lib.ts" />

class Unit {
    
    static FORWARD = 0;
    static RIGHTWARD = 1;
    static BACKWARD = 2;
    static LEFTWARD = 3;
    
    private _body : b2Body;
    private _skin : DisplayObject;
    private _speed = 100;
    private _direction = Unit.FORWARD;
    
    constructor(x : number, y : number) {
	
	    var bodyDef = new b2BodyDef();
	    var shape = new b2PolygonShape();
	    var fixDef = new b2FixtureDef();
    	
	    bodyDef.type = b2Body.b2_dynamicBody;
		bodyDef.position.x = x;
		bodyDef.position.y = y;
		
		this._skin = new createjs.Bitmap("js/img/PlateBlock.jpg");
		this._skin.x = 1;
		this._skin.y = 1;
		this._skin.regX = 12;
		this._skin.regY = 12;
		this._skin.snapToPixel = true;
		this._skin.mouseEnabled = false;
		
		this._body = Game.getInstance().createDynamic(bodyDef, this._skin);
		
		shape.SetAsBox(20, 25);
	    
	    fixDef.shape = shape;
	    fixDef.friction = 1;
	    fixDef.restitution = 0;
	    fixDef.filter.categoryBits = Category.UNIT; 
	    fixDef.filter.maskBits = Category.BLOCK + Category.WATER + Category.UNIT;
	    
		this._body.CreateFixture(fixDef);
		this._body.SetSleepingAllowed(false);
		this._body.SetUserData(this);
		
		this._skin.x = x;
		this._skin.y = y;
    }
    
    public update() {
    	this._skin.x = this._body.GetWorldCenter().x;
    	this._skin.y = this._body.GetWorldCenter().y;
    }
    
    public Move(dir : number) {
    	if(this._direction === dir) {
    		var v = this.countVelocityVec(dir)
    		this._body.SetLinearVelocity(v);
    	}
    	
        var x = this._body.GetPosition().x;
        var y = this._body.GetPosition().y;
        
       	console.log(this.countVelocityVec(dir));
        this._body.SetLinearVelocity(this.countVelocityVec(dir));
        this._body.SetAngle(toRad(dir * 90));
        this._body.SetPosition(this.fixPos(dir));
    }
    
    public Stop() {
    	this._body.SetLinearVelocity(new b2Vec2(0, 0));
    }
    
    private countVelocityVec(dir: number) : b2Vec2 {
        switch (dir) {
            case Unit.FORWARD:
                return new b2Vec2(0, -this._speed);
            case Unit.RIGHTWARD:
                return new b2Vec2(this._speed, 0);
            case Unit.BACKWARD:
                return new b2Vec2(0, this._speed);
            case Unit.LEFTWARD:
                return new b2Vec2(-this._speed, 0);
		}
	}
	
	private fixPos(dir: number) : b2Vec2 {
		var x = this._body.GetPosition().x;
		var y = this._body.GetPosition().y;
		
		if(dir === Unit.FORWARD || dir === Unit.BACKWARD) {
			var xd = x % 24;
			if(xd >= (24 / 2))
				x = x + (24 - xd);
			else 
				x = x - xd;
			return new b2Vec2(x, y);
		}
		else if(dir === Unit.RIGHTWARD || dir === Unit.LEFTWARD) {
			var yd = y % 24;
			if(yd >= (24 / 2))
				y = y + (24 - yd);
			else 
				y = y - yd;
			return new b2Vec2(x, y);
		}
		
		return  new b2Vec2(x, y);
	}
}	